<?PHP
session_start();
include("../config/connect.php");
include("../public/common.php");

if( !isset($_SESSION["gs_user"]) ) 
{
	$url = "Location:login.php";
	header($url);
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>购物兔</title>
<style type="text/css">
<!--
body,td,th {
	font-family: 宋体;
	font-size: 13px;
	color: #000;
}
body {
	background-color: #FFF;
}
-->
</style>
</head>
<body>
<?php
$id = CheckUrlParamNoDie( "id" );
$trade_id = CheckUrlParamNoDie( "trade" );
$szItem = "";
$tb_id = "";
$info = "";
$phone = "";
$all_return = 0;
$has_return = 0;
$b_return = 0;
$price = 0;
$nPage = CheckUrlParamNoDie( "page" );
$showtype = CheckUrlParamNoDie( "showtype" );

if( isset( $_POST["submit"] ) )
{
	$sql = "select * from return_record where trade_id='$trade_id'";
	$result = mysql_query( $sql );
	$row_num = mysql_num_rows( $result );
	
	$szItem = GetPostParamStr("item_id");
	$tb_id = GetPostParamStr("tb");
	$phone = GetPostParamStr("phone");
	$info = GetPostParamStr("info");
	$all_return = GetPostParamFloat("all_return");
	
	$has_return = GetPostParamFloat("has_return");
	$b_return = GetPostParamStr("b_return");
	
	if( $b_return == "on" )
		$b_return = 1;
	else
		$b_return = 0;
	$price = GetPostParamFloat("price");
	
	if( $row_num == 0 )
	{
		$sql = "insert into return_record( tb_id, phone, all_return, has_return, b_return, price, item_id, trade_id, info ) values('$tb_id', '$phone', $all_return, $has_return, $b_return, $price, '$szItem', '$trade_id', '$info')";
		$result = mysql_query( $sql );
	}
	else
	{
		if ($id != "")
			$sql = "update return_record set all_return=$all_return, price=$price, has_return=$has_return, b_return=$b_return, info='$info' where trade_id='$trade_id' and id='$id'";
		else
			$sql = "update return_record set all_return=$all_return, price=$price, has_return=$has_return, b_return=$b_return, info='$info' where trade_id='$trade_id'";
		
		$result = mysql_query( $sql );
	}
	
	echo "<script language=\"javascript\">	alert(\"保存成功\");</script>";
}

$sql = "";
if ($id != "")
	$sql = "select * from return_record where id='$id'";
else
	$sql = "select * from return_record where trade_id='$trade_id'";
	
$result = mysql_query( $sql );
$row_num = 0;
if( $result )
	$row_num = mysql_num_rows( $result );
if( $row_num != 0 )
{
	$row = mysql_fetch_assoc( $result );
	$szItem = $row["item_id"];
	$tb_id = $row["tb_id"];
	$phone = $row["phone"];
	$all_return = $row["all_return"];
	$has_return = $row["has_return"];
	$b_return = $row["b_return"];
	$price = $row["price"];
	$info = $row["info"];
}
else
{
	$sql = "select * from trade where has_return=0 and trade_id='$trade_id' order by id desc";
	$result = mysql_query( $sql );
	while( $result && $row = mysql_fetch_assoc( $result ))
	{
		$szItem .= $row["item_id"] . ",";
		if( $tb_id == "" )
			$tb_id = $row["tb_id"];
			
		$all_return += $row["discount"];
	}
	$sql = "select * from user where tb_id='$tb_id' ";
	$result = mysql_query( $sql );
	if( $result && $row = mysql_fetch_assoc( $result ) )
	{
		$phone = $row["phone"];
	}
}
?><br />
<a href="view_discount.php?page=<?PHP echo $nPage; ?>&showtype=<?php echo $showtype; ?>">返回</a>
<form action="view_trade.php?page=<?PHP echo $nPage; ?>&id=<?PHP echo $id; ?>&trade=<?PHP echo $trade_id; ?>&showtype=<?php echo $showtype; ?>" method="post">
<table cellpadding="0" cellspacing="0" border="1" align="center">
	<tr>
    	<td width="60" height="30" align="center">物品ID：</td>
        <td colspan="5"><input type="text" name="item_id" value="<?PHP echo $szItem; ?>" size="70"/>&nbsp;</td>
    </tr>
    <tr>
   	  <td width="60" height="30" align="center">订单号：</td>
      <td width="140" height="22"><input type="text" width="140" id="trade_id" name="trade" height="20" value="<?PHP echo $trade_id; ?>"  /></td>
      <td width="58" height="30" align="center">账号：</td>
        <td width="152" height="22"><input type="text" width="140" id="tb_id" name="tb" height="20" value="<?PHP echo $tb_id; ?>" /></td>
      <td width="55" height="30" align="center">手机号：</td>
      <td width="145" height="22"><input type="text" width="140" id="phone_id" name="phone" height="20" value="<?PHP echo $phone; ?>" /></td>
    </tr>
    <tr>
    	<td colspan="2" height="30" align="right"s>共需返还金额：</td>
        <td colspan="4"><input type="text"  width="340" id="all_return_id" name="all_return" height="20" value="<?PHP echo $all_return; ?>" />&nbsp;</td>
    </tr>
    <tr>
    	<td colspan="2" height="30"  align="right"s>成交额：</td>
        <td colspan="4"><input type="text"  width="340" id="price_id" name="price" height="20" value="<?PHP echo $price; ?>" />&nbsp;</td>
    </tr>
    <tr>
    	<td colspan="2" height="30" align="right"s>已返现：</td>
        <td colspan="4"><input type="text"  width="340" id="has_return_id" name="has_return" height="20" value="<?PHP echo $has_return; ?>" />&nbsp;</td>
    </tr>
    <tr>
    	<td colspan="2" height="30" align="right"s>是否返全：</td>
        <td colspan="4"><input type="checkbox" id="b_return_id" name="b_return" <?PHP if( $b_return == 1 )	echo "checked='checked'"; ?> />&nbsp;</td>
    </tr>
    <tr>
    	<td colspan="2" height="30" align="right"s>备注：</td>
        <td colspan="4"><textarea name="info" cols="56" rows="7"><?PHP echo $info; ?></textarea></td>
    </tr>
    <tr>
    	<td colspan="6" height="30" align="center"><input type="submit" name="submit" value="保存" style=" width:80px; height:23px"/></td>
    </tr>
</table>
</form>
</body>
</html>
